import Vue from 'vue'
import VueRouter from 'vue-router'
// import Films from '@/views/Films'
// import Center from '@/views/Center'
// import Cinema from '@/views/Cinema'
import NowPlaying from '@/views/films/NowPlaying'
import Coming from '@/views/films/Coming'
import Error from '@/views/Error'
import Detail from '@/views/Detail'
Vue.use(VueRouter)

  const routes = [
  {
    path:'/films',
    component:()=>import(/* webpackChunkName:'Films'  */'@/views/Films'),
    //进行二级路由配置
    children:[
        {
          path:'nowplaying',
          component:NowPlaying
        },
        {
          path:'coming',
          component:Coming
        },
        {
          path:"",
          redirect:"/films/nowplaying"
        }

    ]
  },
  // {
  //   path:'/films/nowplaying',
  //   component:NowPlaying
  // },
  // {
  //   path:'/films/coming',
  //   component:Coming
  // },
  {
    path:'/center',
    component:()=>import('@/views/Center'),
    // beforeEnter(to,from,next){
    //   console.log("进入center了哦")
    //   next()
    // }
  },
  {
    path:'/cinema',
    component:()=>import("@/views/Cinema")
  },
  {
    path:'/city',
    component:()=>import("@/views/City")
  },
  {
    path:"/",
    redirect:"/films"
  },
  {
    path:"*",
    component:Error
  },
  {
    path:'/login',
    component:()=>import('@/views/login')
  },
  {
    name:'detail',
    path:"/detail/:id",
    component:Detail,
    props:true
  },
  {
    path:'/cinema/search',
    component:()=>import('@/views/Search')
  },
]

const router = new VueRouter({
  mode:"history",
  routes
})

//全局路由守卫
//全局路由的前置守卫
// router.beforeEach((to,from,next)=>{
//   if(from.path==="/cinema"){
//     console.log("从影院里面过来买菜的")
//   }
//   next()
// })
//全局的后置路由守卫
// router.afterEach((to,from)=>{
//   if(to.path==="/center"){
//     console.log("进入center了")
//   }
// })

//判断每次路由切换的时候判断是否有token令牌
router.beforeEach((to,from,next)=>{
  if(to.path==="/center"){
    if(localStorage.getItem("token")){//说明用户已经登陆
      next()
    }else{
      next("/login")//如果用户没有登录直接放进login页面
    }
  }else{
    next()
  }
})
export default router
